package com.todo.strategy.online.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.strategy.online.infrastructure.convert.MemoConvert;
import com.todo.strategy.online.infrastructure.entity.domain.MemoTemplateDO;
import com.todo.strategy.online.infrastructure.entity.po.MemoTemplatePO;
import com.todo.strategy.online.infrastructure.mapper.MemoTemplateMapper;
import com.todo.strategy.online.infrastructure.repository.MemoTemplateRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2024/11/28 02:11
 * @description
 */
@Slf4j
@Repository("memoTemplateRepository")
public class MemoTemplateRepositoryImpl implements MemoTemplateRepository {

    @Resource
    private MemoTemplateMapper memoTemplateMapper;

    @Override
    public Boolean insertMemoTemplate(MemoTemplateDO memoTemplateDO) {
        if (memoTemplateDO == null) {
            return false;
        }
        if (!baseFieldCheck(memoTemplateDO)) {
            return false;
        }
        MemoTemplatePO memoTemplatePO = MemoConvert.convertToMemoTemplatePO(memoTemplateDO);
        return memoTemplateMapper.insertMemoTemplatePO(memoTemplatePO) > 0;
    }

    @Override
    public Boolean updateMemoTemplate(MemoTemplateDO memoTemplateDO) {
        if (memoTemplateDO == null || memoTemplateDO.getId() == null) {
            return false;
        }
        //更新操作是根据是否有值确定的，所以无需做数据校验了
        MemoTemplatePO memoTemplatePO = MemoConvert.convertToMemoTemplatePO(memoTemplateDO);
        return memoTemplateMapper.updateMemoTemplatePOById(memoTemplatePO) > 0;
    }

    @Override
    public MemoTemplateDO queryMemoTemplateDOById(Long templateId) {
        if (templateId == null) {
            return null;
        }
        MemoTemplatePO memoTemplatePO = memoTemplateMapper.queryMemoTemplatePOById(templateId);
        return MemoConvert.convertToMemoTemplateContentDO(memoTemplatePO);
    }

    @Override
    public Boolean removeMemoTemplateDOById(Long id) {
        if (id == null || id < 0) {
            return false;
        }
        return memoTemplateMapper.removeMemoTemplatePOById(id) > 0;
    }

    @Override
    public Boolean updateMemoStatusById(Long id, int oldStatus, int newStatus) {
        if (id == null || id < 0) {
            return false;
        }
        return memoTemplateMapper.updateMemoStatusById(id, oldStatus, newStatus);
    }

    @Override
    public List<MemoTemplateDO> queryMemoTemplateDOByUserId(Long userId) {
        if (userId == null || userId < 0){
            return Collections.emptyList();
        }
        List<MemoTemplatePO> memoTemplatePOList = memoTemplateMapper.queryMemoTemplateDOByUserId(userId);
        if (CollectionUtils.isEmpty(memoTemplatePOList)){
            return Collections.emptyList();
        }
        return memoTemplatePOList.stream().map(MemoConvert::convertToMemoTemplateContentDO).collect(Collectors.toList());
    }

    @Override
    public List<MemoTemplateDO> queryMemoTemplateDOListByIdList(List<Long> templateIdList) {
        if (CollectionUtils.isEmpty(templateIdList)){
            return Collections.emptyList();
        }
        List<MemoTemplatePO> memoTemplatePOList = memoTemplateMapper.queryMemoTemplatePOListByIdList(templateIdList);
        if (CollectionUtils.isEmpty(memoTemplatePOList)){
            return Collections.emptyList();
        }
        return memoTemplatePOList.stream().map(MemoConvert::convertToMemoTemplateContentDO).collect(Collectors.toList());
    }


    private boolean baseFieldCheck(MemoTemplateDO memoTemplateDO) {
        if (memoTemplateDO == null) {
            return false;
        }
        if (memoTemplateDO.getUserId() == null || memoTemplateDO.getName() == null || memoTemplateDO.getContentModel() == null) {
            log.warn("MemoTemplateRepository#baseFieldCheck 核心字段校验失败，默认操作失败 memoTemplateContentDO：【{}】", JSONObject.toJSONString(memoTemplateDO));
            return false;
        }
        if (memoTemplateDO.getBaseType() == null || memoTemplateDO.getSuggestStrategy() == null || memoTemplateDO.getIsPlatform() == null) {
            log.warn("MemoTemplateRepository#baseFieldCheck 核心字段校验失败，默认操作失败 memoTemplateContentDO：【{}】", JSONObject.toJSONString(memoTemplateDO));
            return false;
        }
        return true;
    }
}
